package com.szqhyxj.youxiangju.biz.merchantHome.home;


import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.ms.banner.Banner;
import com.ms.banner.BannerConfig;
import com.ms.banner.Transformer;
import com.ms.banner.holder.BannerViewHolder;
import com.ms.banner.holder.HolderCreator;
import com.ms.banner.listener.OnBannerListener;
import com.szqhyxj.youxiangju.BasePromptFragment;
import com.szqhyxj.youxiangju.R;
import com.szqhyxj.youxiangju.biz.card.cardApply.CardApplyActivity;
import com.szqhyxj.youxiangju.biz.core.TokenManager;
import com.szqhyxj.youxiangju.biz.creditCardEvaluation.CreditCardEcaluationActivity;
import com.szqhyxj.youxiangju.biz.equipmentActivation.equipmentCardslot.EquipmentCardSlotActivity;
import com.szqhyxj.youxiangju.biz.login.LoginActivity;
import com.szqhyxj.youxiangju.biz.merchantHome.deviceinfo.DeviceInfo;
import com.szqhyxj.youxiangju.biz.merchantHome.home.ui.CustomViewHolder;
import com.szqhyxj.youxiangju.biz.plan.RepaymentActivity;
import com.szqhyxj.youxiangju.biz.realNameAuth.idcard.IDCardAuthActivity;
import com.szqhyxj.youxiangju.biz.receipt.ReceiptActivity;
import com.szqhyxj.youxiangju.biz.web.ApplyCardMyWebActivity;
import com.szqhyxj.youxiangju.biz.web.WebActivity;
import com.szqhyxj.youxiangju.sys.components.view.popview.GoShopPopView;
import com.szqhyxj.youxiangju.sys.components.view.popview.RealNamePopView;
import com.szqhyxj.youxiangju.sys.components.view.popview.ToBeVipPopView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * Created by leung on 2018/10/30.
 */
public class MerchantHomeFragment extends BasePromptFragment implements HomeContract.View {


    @BindView(R.id.banner)
    Banner banner;

    @BindView(R.id.repay)
    LinearLayout repay;
    @BindView(R.id.review_creditcard)
    LinearLayout reviewCreditcard;
    @BindView(R.id.apply_card)
    LinearLayout applyCard;
    @BindView(R.id.credit_card_banner)
    Banner creditCardBanner;
    @BindView(R.id.telephone)
    LinearLayout telephone;
    @BindView(R.id.netArea)
    LinearLayout netarea;
    @BindView(R.id.nettitle)
    TextView nettitle;
    @BindView(R.id.neticon)
    ImageView neticon;
    Unbinder unbinder;
    @BindView(R.id.vipachievement)
    LinearLayout vipachievement;
    @BindView(R.id.partnerperformance)
    LinearLayout partnerperformance;
    @BindView(R.id.partnerarea)
    LinearLayout partnerarea;
    @BindView(R.id.receipt)
    LinearLayout receipt;
    @BindView(R.id.partnerview)
    View partnerview;
    @BindView(R.id.share)
    ImageView share;
    @BindView(R.id.certified)
    TextView certified;
    @BindView(R.id.surplus)
    TextView surplus;
    @BindView(R.id.profitSum)
    TextView profitSum;
    @BindView(R.id.company)
    TextView company;
    @BindView(R.id.vipArea)
    LinearLayout vipArea;
    @BindView(R.id.linevip)
    View linevip;


    private List<ImageView> indicatorImages = new ArrayList<>();
    private int lastPosition = 0;
    private static final String TAG = MerchantHomeFragment.class.getSimpleName();

    private HomeContract.Presenter presenter;

    public static MerchantHomeFragment newInstance() {
        Bundle args = new Bundle();
        MerchantHomeFragment fragment = new MerchantHomeFragment();
        fragment.setArguments(args);

        return fragment;
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        if (presenter == null) {
            presenter = new HomePresenter(this);

        }
        if (TokenManager.INSTANCE.getMToken() == null || TokenManager.INSTANCE.getMToken().equals("")) {

        } else {

            presenter.getVipInfo();
        }
        super.setUserVisibleHint(isVisibleToUser);
    }


    @Override
    protected void start() {
        presenter.loadBanner();
        presenter.deviceinit();


    }

    @Override
    protected int getLayoutId() {

        return R.layout.fragment_home;
    }

    @Override
    public void guideToRealName() {
        final RealNamePopView popView = new RealNamePopView(getActivity());
        popView.setOnClickListener(new RealNamePopView.OnClickListener() {
            @Override
            public void onClickConfirm(View v) {
                IDCardAuthActivity.start(getActivity());
                popView.dismiss();
            }
        });

        popView.show();
    }

    @Override
    public void navToLogin() {
        LoginActivity.start(getActivity());
    }

    @Override
    public void showBanner(List<Integer> bannerlist1, List<Integer> bannerlist2) {
        banner.setPages(bannerlist1, new HolderCreator<BannerViewHolder>() {
            @Override
            public BannerViewHolder createViewHolder() {
                return new CustomViewHolder();
            }
        })
                .setOnBannerListener(new OnBannerListener() {
                    @Override
                    public void onBannerClick(int position) {

                    }
                })
                .setAutoPlay(true)
                .setDelayTime(5000)
                .start();
        banner.setIndicatorRes(R.drawable.gray_rellipse, R.drawable.gray_radius);
        banner.updateBannerStyle(BannerConfig.CUSTOM_INDICATOR);
        creditCardBanner.setAutoPlay(true)
                .setOffscreenPageLimit(bannerlist2.size())
                .setPages(bannerlist2, new HolderCreator<BannerViewHolder>() {
                    @Override
                    public BannerViewHolder createViewHolder() {
                        return new CustomViewHolder();
                    }
                })
                .setBannerStyle(BannerConfig.NOT_INDICATOR)
                .setBannerAnimation(Transformer.Scale)
                .setDelayTime(8000)
                .setOnBannerListener(new OnBannerListener() {
                    @Override
                    public void onBannerClick(int position) {
                        if (position == 0) {
                            ApplyCardMyWebActivity.start(getActivity(), "申请信用卡", "http://ads.imoneybox.cn/admin/pub/gotoweb.html?pathid=78");
                        }
                        if (position == 1) {
                            ApplyCardMyWebActivity.start(getActivity(), "申请信用卡", "http://ads.imoneybox.cn/admin/pub/gotoweb.html?pathid=80");
                        }
                    }
                })
                .start();
        creditCardBanner.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                // Toast.makeText(CustomViewPagerActivity.this,"aa",Toast.LENGTH_LONG).show();
            }

            @Override
            public void onPageSelected(int position) {
               /* indicatorImages.get((lastPosition + 2) % 2).setImageResource(R.drawable.gray_rellipse);
                indicatorImages.get((position + 2) % 2).setImageResource(R.drawable.gray_radius);
                lastPosition = position;*/
            }

            @Override
            public void onPageScrollStateChanged(int state) {
                //  Toast.makeText(CustomViewPagerActivity.this,"aa",Toast.LENGTH_LONG).show();
            }
        });
    }

    @Override
    public void showBanner2(List<String> banner1, List<Integer> banner2) {
        banner.setPages(banner1, new HolderCreator<BannerViewHolder>() {
            @Override
            public BannerViewHolder createViewHolder() {
                return new CustomViewHolder();
            }
        })
                .setOnBannerListener(new OnBannerListener() {
                    @Override
                    public void onBannerClick(int position) {

                    }
                })
                .setAutoPlay(true)
                .setDelayTime(5000)
                .start();
        banner.setIndicatorRes(R.drawable.gray_rellipse, R.drawable.gray_radius);
        banner.updateBannerStyle(BannerConfig.CUSTOM_INDICATOR);
        creditCardBanner.setAutoPlay(true)
                .setOffscreenPageLimit(banner2.size())
                .setPages(banner2, new HolderCreator<BannerViewHolder>() {
                    @Override
                    public BannerViewHolder createViewHolder() {
                        return new CustomViewHolder();
                    }
                })
                .setBannerStyle(BannerConfig.NOT_INDICATOR)
                .setBannerAnimation(Transformer.Scale)
                .setDelayTime(8000)
                .setOnBannerListener(new OnBannerListener() {
                    @Override
                    public void onBannerClick(int position) {
                        if (position == 0) {
                            ApplyCardMyWebActivity.start(getActivity(), "申请信用卡", "http://ads.imoneybox.cn/admin/pub/gotoweb.html?pathid=78");
                        }
                        if (position == 1) {
                            ApplyCardMyWebActivity.start(getActivity(), "申请信用卡", "http://ads.imoneybox.cn/admin/pub/gotoweb.html?pathid=80");
                        }
                    }
                })
                .start();
        creditCardBanner.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                // Toast.makeText(CustomViewPagerActivity.this,"aa",Toast.LENGTH_LONG).show();
            }

            @Override
            public void onPageSelected(int position) {
               /* indicatorImages.get((lastPosition + 2) % 2).setImageResource(R.drawable.gray_rellipse);
                indicatorImages.get((position + 2) % 2).setImageResource(R.drawable.gray_radius);
                lastPosition = position;*/
            }

            @Override
            public void onPageScrollStateChanged(int state) {
                //  Toast.makeText(CustomViewPagerActivity.this,"aa",Toast.LENGTH_LONG).show();
            }
        });

    }

    @Override
    public void showBanner(List<HomeModel.AdBannerItem> listBaseResult) {

/*
        banner.setPages(mIntegerList, new HolderCreator<BannerViewHolder>() {
            @Override
            public BannerViewHolder createViewHolder() {
                return new CustomViewHolder();
            }
        })
                .setOnBannerListener(new OnBannerListener() {
                    @Override
                    public void onBannerClick(int position) {
                        Toast.makeText(getContext(), "您点击了" + position, Toast.LENGTH_SHORT).show();
                    }
                })
                .setAutoPlay(true)
                .setDelayTime(5000)
                .start();
        banner.setIndicatorRes(R.drawable.gray_rellipse, R.drawable.gray_radius);
        banner.updateBannerStyle(BannerConfig.CUSTOM_INDICATOR);


        creditCardBanner.setAutoPlay(true)
                .setOffscreenPageLimit(list.size())
                .setPages(list, new HolderCreator<BannerViewHolder>() {
                    @Override
                    public BannerViewHolder createViewHolder() {
                        return new CustomViewHolder();
                    }
                })
                .setBannerStyle(BannerConfig.NOT_INDICATOR)
                .setBannerAnimation(Transformer.Scale)
                .setDelayTime(8000)
                .setOnBannerListener(new OnBannerListener() {
                    @Override
                    public void onBannerClick(int position) {


                    }
                })
                .start();
        creditCardBanner.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                // Toast.makeText(CustomViewPagerActivity.this,"aa",Toast.LENGTH_LONG).show();
            }

            @Override
            public void onPageSelected(int position) {
               *//* indicatorImages.get((lastPosition + 2) % 2).setImageResource(R.drawable.gray_rellipse);
                indicatorImages.get((position + 2) % 2).setImageResource(R.drawable.gray_radius);
                lastPosition = position;*//*
            }

            @Override
            public void onPageScrollStateChanged(int state) {
                //  Toast.makeText(CustomViewPagerActivity.this,"aa",Toast.LENGTH_LONG).show();
            }
        });*/
       /* List< RequestBuilder<Drawable>> drawables = new ArrayList<>();
        for (int i = 0;i<listBaseResult.size();i++){
            RequestBuilder<Drawable> drawable = Glide.with(this).load(listBaseResult.get(i));
            drawables.add(drawable);
        }

       banner.setPages(drawables, new HolderCreator<BannerViewHolder>() {
           @Override
           public BannerViewHolder createViewHolder() {
               return new CustomViewHolder();
           }
       }).setOnBannerListener(new OnBannerListener() {
           @Override
           public void onBannerClick(int i) {
               Toast.makeText(getContext(), "您点击了" + i, Toast.LENGTH_SHORT).show();
           }
       }).setAutoPlay(true)
               .setDelayTime(5000)
               .loadPlanDetail();
        banner.setIndicatorRes(R.drawable.gray_rellipse, R.drawable.gray_radius);
        banner.updateBannerStyle(BannerConfig.CUSTOM_INDICATOR);*/


    }


    @Override
    public void showCreditCardBanner(List<HomeModel.BankBannerItem> listBaseResult) {

    }

    @Override
    public void navToCashout() {
        ReceiptActivity.start(getActivity());
      /*  WebActivity.startToH5(getActivity(),"createplan?type=offline&&billDate=10&&repayDate=25&&title=建设银行(1293)&&channelKey=jianlian&&channelName=银联快捷&&merchantArea=深圳南山&&cardNo=610502192312313257812");*/
    }


    @Override
    public void navToRepay() {
        RepaymentActivity.start(getActivity());

    }

    @Override
    public void navToCreditCardReview() {
        CreditCardEcaluationActivity.start(getActivity());
    }

    @Override
    public void navToApplyCreditCard() {
        CardApplyActivity.start(getActivity());
    }

    @Override
    public void navToPartnerperformance() {

        WebActivity.startToH5(getActivity(), "partnerPerformance");
    }

    @Override
    public void navToVipachievement() {
        WebActivity.startToH5(getActivity(), "vipData");
    }

    @Override
    public void goshop() {
        final GoShopPopView popView = new GoShopPopView(getActivity());
        popView.setOnClickListener(new GoShopPopView.OnClickListener() {
            @Override
            public void onClickConfirm(View v) {
                popView.dismiss();
                if (MerchantHomeFragment.this.goShopListener == null) {
                    return;
                }
                MerchantHomeFragment.this.goShopListener.goShop();
            }
        });
        popView.show();
    }

    public interface GoShopListener {
        void goShop();
    }

    private GoShopListener goShopListener;

    public void setGoShopListener(GoShopListener goShopListener) {
        this.goShopListener = goShopListener;
    }

    @Override
    public void showLoadBannerFail(String message) {
        Toast.makeText(getActivity(), message, Toast.LENGTH_LONG).show();
    }

    @Override
    public void showDeviceInfo() {
        netarea.setVisibility(View.VISIBLE);
        if (DeviceInfo.Holder.getInstance().getOnlineState().equals("1")) {
            neticon.setImageDrawable(getResources().getDrawable(R.mipmap.poswangluo));
            nettitle.setText(DeviceInfo.Holder.getInstance().getAepnType());
            nettitle.setTextColor(getResources().getColor(R.color.white));
        } else {
            neticon.setImageDrawable(getResources().getDrawable(R.mipmap.poswangluo1));
            nettitle.setText("离线");
            nettitle.setTextColor(getResources().getColor(R.color.texttm));
        }
    }

    @Override
    public void enshowDeviceInfo() {
        netarea.setVisibility(View.GONE);
    }

    @Override
    public void showPartnerArea() {
        partnerview.setVisibility(View.VISIBLE);
        partnerarea.setVisibility(View.VISIBLE);
    }

    @Override
    public void enshowPartnerArea() {
        partnerview.setVisibility(View.GONE);
        partnerarea.setVisibility(View.GONE);
    }

    @Override
    public void gotoshare() {
        WebActivity.startToH5(getActivity(), "RewardDescription");
    }

    @Override
    public void navToBeVipachievement() {
        final ToBeVipPopView popView = new ToBeVipPopView(getActivity());
        popView.setOnClickListener(new ToBeVipPopView.OnClickListener() {
            @Override
            public void onClickConfirm(View v) {
                popView.dismiss();
                WebActivity.startToH5(getActivity(), "RewardDescription");
            }
        });
        popView.show();
    }

    @Override
    public void showVipArea(HomeModel.Vipentity vipentity) {
        vipArea.setVisibility(View.VISIBLE);
        linevip.setVisibility(View.GONE);
        certified.setText(vipentity.getCertified());
        surplus.setText(vipentity.getSurplus());
        if (vipentity.getCertified().equals("0")) {
            profitSum.setVisibility(View.GONE);
            company.setVisibility(View.GONE);
        } else {
            profitSum.setVisibility(View.VISIBLE);
            company.setVisibility(View.VISIBLE);
            profitSum.setText(vipentity.getProfitSum());
        }

    }

    @Override
    public void enshowVipArea() {
        vipArea.setVisibility(View.GONE);
        linevip.setVisibility(View.VISIBLE);
    }


    @OnClick({R.id.receipt, R.id.repay, R.id.review_creditcard, R.id.apply_card, R.id.telephone, R.id.netArea, R.id.vipachievement, R.id.partnerperformance, R.id.share})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.receipt:
                presenter.receipt();
                break;
            case R.id.repay:
                presenter.repay();
                break;
            case R.id.review_creditcard:
                presenter.creditCardReview();
                break;
            case R.id.apply_card:
                presenter.applyCreditCard();
                break;
            case R.id.telephone:
                break;
            case R.id.netArea:
                EquipmentCardSlotActivity.start(getActivity());
                break;
            case R.id.vipachievement:
                presenter.vipachievement();
                break;
            case R.id.partnerperformance:
                presenter.partnerperformance();
                break;
            case R.id.share:
                presenter.share();
                break;
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        unbinder = ButterKnife.bind(this, rootView);

        return rootView;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }
}
